package com.hz.email.controller;

import com.github.pagehelper.PageInfo;
import com.hz.email.entity.CommonResult;
import com.hz.email.entity.TEmail;
import com.hz.email.entity.TResult;
import com.hz.email.service.EmailService;
import com.hz.email.service.ResultService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ResultController {
    @Resource
    private ResultService resultService;

    @Resource
    @Qualifier(value = "EmailServiceImpl1")
    private EmailService emailService;

    @PostMapping(value = "/findAll")
    public CommonResult findAll(@RequestBody TResult tResult) {
        PageInfo pageInfo = resultService.findAll(tResult);
        return CommonResult.successful(pageInfo);
    }

    @PostMapping(value = "/addEmail")
    public CommonResult addEmail(@RequestBody TEmail tEmail) {
        int temp = 0;
        try {
            temp = emailService.addEmail(tEmail);
            if (temp >= 1) {
                return CommonResult.successful(temp);
            } else {
                return CommonResult.failwith(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failwith(temp);
    }

    @PostMapping(value = "findIn")
    public CommonResult findIn(@RequestBody TEmail tEmail) {
        List<TEmail> list = emailService.findIn(tEmail);
        if (list.size() == 0) {
            return CommonResult.successful(200, "请求为空", null);
        }
        return CommonResult.successful(list);
    }


    @PostMapping(value = "findResultIn")
    public CommonResult findResultIn(@RequestBody TResult tResult) {
        if (0 == tResult.getIds().size() || null == tResult.getIds()) {
            return CommonResult.successful(200, "请求为空", null);
        }
        List<TResult> resultList = resultService.findIn(tResult);
        if (resultList.size() == 0) {
            return CommonResult.successful(200, "请求为空", null);
        }
        return CommonResult.successful(resultList);
    }

}
